﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using TicTacToe.Core.Persistance;

namespace TicTacToe.UI
{
    internal partial class LoadGameWindow : Window
    {
        bool _starting = false;

        sealed class SGWrapper
        {
            public readonly SavedGame Game;

            public Brush Player1Color { get { return Game.Player1.Color.CreateBrush(); } set { } }
            public Brush Player2Color { get { return Game.Player2.Color.CreateBrush(); } set { } }
            public string Player1Name { get { return Game.Player1.Name; } set { } }
            public string Player2Name { get { return Game.Player2.Name; } set { } }
            public int TurnCount { get { return Game.Turns.Count(); } set { } }
            public DateTime SaveTime { get { return Game.SaveTime; } set { } }

            public SGWrapper(SavedGame game)
            {
                Game = game;
            }
        }

        void LoadSaveGames()
        {
            Task.Factory.StartNew<IEnumerable<SavedGame>>(() =>
            {
                return GameManager.GetAllGames();
            }).ContinueWith((task) =>
            {
                Action<SGWrapper> add = (sgw) => savesListBox.Items.Add(sgw);

                foreach (var game in task.Result.OrderByDescending(n => n.SaveTime))
                    Dispatcher.Invoke(add, new SGWrapper(game));
            });;
        }

        public LoadGameWindow()
        {
            InitializeComponent();
            LoadSaveGames();
        }

        private void deleteSelectedButton_Click(object sender, RoutedEventArgs e)
        {
            var selection = savesListBox.SelectedItem as SGWrapper;
            if (selection != null)
            {
                GameManager.DeleteSavedGame(selection.Game);
                savesListBox.Items.Remove(selection);
            }
        }

        private void deleteAllButton_Click(object sender, RoutedEventArgs e)
        {
            var result = MessageBox.Show("Are you sure? This will irrevocably delete all saved games.",
                                         "Delete all saved games?",
                                         MessageBoxButton.YesNo,
                                         MessageBoxImage.Warning);
            if (result == MessageBoxResult.No)
                return;

            foreach (var item in savesListBox.Items.Cast<SGWrapper>())
                GameManager.DeleteSavedGame(item.Game);
            savesListBox.Items.Clear();
        }

        private void loadGameButton_Click(object sender, RoutedEventArgs e)
        {
            var selection = savesListBox.SelectedItem as SGWrapper;
            if (selection == null)
                MessageBox.Show("No game selected. Select a saved game and try again.");
            else
            {
                _starting = true;
                new GameWindow(selection.Game).Show();
                Close();
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!_starting)
                new StartWindow().Show();
        }
    }
}
